using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;


namespace CUSTOMS.Modules.QLHS.Components.QLBB
{
	public partial class HangHoa
	{
		#region Private members.
		
		protected int _SoToKhai;
		protected string _MaHaiQuan = string.Empty;
		protected string _MaLoaiHinh = string.Empty;
		protected int _NamDangKy;
		protected int _STT;
		protected string _SoHieuContainer = string.Empty;
		protected string _SoSealContainer = string.Empty;
		protected string _SoSealHaiQuan = string.Empty;
		protected string _GhiChu = string.Empty;

		#endregion
		
		//---------------------------------------------------------------------------------------------

		#region Properties.
		
		public int SoToKhai
		{
			set {this._SoToKhai = value;}
			get {return this._SoToKhai;}
		}
		
		public string MaHaiQuan
		{
			set {this._MaHaiQuan = value;}
			get {return this._MaHaiQuan;}
		}
		
		public string MaLoaiHinh
		{
			set {this._MaLoaiHinh = value;}
			get {return this._MaLoaiHinh;}
		}
		
		public int NamDangKy
		{
			set {this._NamDangKy = value;}
			get {return this._NamDangKy;}
		}
		
		public int STT
		{
			set {this._STT = value;}
			get {return this._STT;}
		}
		
		public string SoHieuContainer
		{
			set {this._SoHieuContainer = value;}
			get {return this._SoHieuContainer;}
		}
		
		public string SoSealContainer
		{
			set {this._SoSealContainer = value;}
			get {return this._SoSealContainer;}
		}
		
		public string SoSealHaiQuan
		{
			set {this._SoSealHaiQuan = value;}
			get {return this._SoSealHaiQuan;}
		}
		
		public string GhiChu
		{
			set {this._GhiChu = value;}
			get {return this._GhiChu;}
		}
		
		
		#endregion
		
		//---------------------------------------------------------------------------------------------
		
		#region Select methods.
		
		public static HangHoa Load(int soToKhai, string maHaiQuan, string maLoaiHinh, int namDangKy, int sTT)
		{
			const string spName = "[dbo].[p_HAIQUAN_QLHS_HangHoa_Load]";
            SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
			SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);
			
			db.AddInParameter(dbCommand, "@SoToKhai", SqlDbType.Int, soToKhai);
			db.AddInParameter(dbCommand, "@MaHaiQuan", SqlDbType.Char, maHaiQuan);
			db.AddInParameter(dbCommand, "@MaLoaiHinh", SqlDbType.Char, maLoaiHinh);
			db.AddInParameter(dbCommand, "@NamDangKy", SqlDbType.Int, namDangKy);
			db.AddInParameter(dbCommand, "@STT", SqlDbType.Int, sTT);
			HangHoa entity = null;
            IDataReader reader = db.ExecuteReader(dbCommand);
			if (reader.Read())
			{
				entity = new HangHoa();
				if (!reader.IsDBNull(reader.GetOrdinal("SoToKhai"))) entity.SoToKhai = reader.GetInt32(reader.GetOrdinal("SoToKhai"));
				if (!reader.IsDBNull(reader.GetOrdinal("MaHaiQuan"))) entity.MaHaiQuan = reader.GetString(reader.GetOrdinal("MaHaiQuan"));
				if (!reader.IsDBNull(reader.GetOrdinal("MaLoaiHinh"))) entity.MaLoaiHinh = reader.GetString(reader.GetOrdinal("MaLoaiHinh"));
				if (!reader.IsDBNull(reader.GetOrdinal("NamDangKy"))) entity.NamDangKy = reader.GetInt32(reader.GetOrdinal("NamDangKy"));
				if (!reader.IsDBNull(reader.GetOrdinal("STT"))) entity.STT = reader.GetInt32(reader.GetOrdinal("STT"));
				if (!reader.IsDBNull(reader.GetOrdinal("SoHieuContainer"))) entity.SoHieuContainer = reader.GetString(reader.GetOrdinal("SoHieuContainer"));
				if (!reader.IsDBNull(reader.GetOrdinal("SoSealContainer"))) entity.SoSealContainer = reader.GetString(reader.GetOrdinal("SoSealContainer"));
				if (!reader.IsDBNull(reader.GetOrdinal("SoSealHaiQuan"))) entity.SoSealHaiQuan = reader.GetString(reader.GetOrdinal("SoSealHaiQuan"));
				if (!reader.IsDBNull(reader.GetOrdinal("GhiChu"))) entity.GhiChu = reader.GetString(reader.GetOrdinal("GhiChu"));
			}
			reader.Close();
			return entity;
		}		
		
		//---------------------------------------------------------------------------------------------
		public static IList<HangHoa> SelectCollectionAll()
		{
			IList<HangHoa> collection = new List<HangHoa>();
			IDataReader reader = SelectReaderAll();
			while (reader.Read())
			{
				HangHoa entity = new HangHoa();
				
				if (!reader.IsDBNull(reader.GetOrdinal("SoToKhai"))) entity.SoToKhai = reader.GetInt32(reader.GetOrdinal("SoToKhai"));
				if (!reader.IsDBNull(reader.GetOrdinal("MaHaiQuan"))) entity.MaHaiQuan = reader.GetString(reader.GetOrdinal("MaHaiQuan"));
				if (!reader.IsDBNull(reader.GetOrdinal("MaLoaiHinh"))) entity.MaLoaiHinh = reader.GetString(reader.GetOrdinal("MaLoaiHinh"));
				if (!reader.IsDBNull(reader.GetOrdinal("NamDangKy"))) entity.NamDangKy = reader.GetInt32(reader.GetOrdinal("NamDangKy"));
				if (!reader.IsDBNull(reader.GetOrdinal("STT"))) entity.STT = reader.GetInt32(reader.GetOrdinal("STT"));
				if (!reader.IsDBNull(reader.GetOrdinal("SoHieuContainer"))) entity.SoHieuContainer = reader.GetString(reader.GetOrdinal("SoHieuContainer"));
				if (!reader.IsDBNull(reader.GetOrdinal("SoSealContainer"))) entity.SoSealContainer = reader.GetString(reader.GetOrdinal("SoSealContainer"));
				if (!reader.IsDBNull(reader.GetOrdinal("SoSealHaiQuan"))) entity.SoSealHaiQuan = reader.GetString(reader.GetOrdinal("SoSealHaiQuan"));
				if (!reader.IsDBNull(reader.GetOrdinal("GhiChu"))) entity.GhiChu = reader.GetString(reader.GetOrdinal("GhiChu"));
				collection.Add(entity);
			}
			
			reader.Close();
			return collection;			
		}		
		
		//---------------------------------------------------------------------------------------------
		
		public static IList<HangHoa> SelectCollectionDynamic(string whereCondition, string orderByExpression)
		{
			IList<HangHoa> collection = new List<HangHoa>();

			IDataReader reader = SelectReaderDynamic(whereCondition, orderByExpression);
			while (reader.Read())
			{
				HangHoa entity = new HangHoa();
				
				if (!reader.IsDBNull(reader.GetOrdinal("SoToKhai"))) entity.SoToKhai = reader.GetInt32(reader.GetOrdinal("SoToKhai"));
				if (!reader.IsDBNull(reader.GetOrdinal("MaHaiQuan"))) entity.MaHaiQuan = reader.GetString(reader.GetOrdinal("MaHaiQuan"));
				if (!reader.IsDBNull(reader.GetOrdinal("MaLoaiHinh"))) entity.MaLoaiHinh = reader.GetString(reader.GetOrdinal("MaLoaiHinh"));
				if (!reader.IsDBNull(reader.GetOrdinal("NamDangKy"))) entity.NamDangKy = reader.GetInt32(reader.GetOrdinal("NamDangKy"));
				if (!reader.IsDBNull(reader.GetOrdinal("STT"))) entity.STT = reader.GetInt32(reader.GetOrdinal("STT"));
				if (!reader.IsDBNull(reader.GetOrdinal("SoHieuContainer"))) entity.SoHieuContainer = reader.GetString(reader.GetOrdinal("SoHieuContainer"));
				if (!reader.IsDBNull(reader.GetOrdinal("SoSealContainer"))) entity.SoSealContainer = reader.GetString(reader.GetOrdinal("SoSealContainer"));
				if (!reader.IsDBNull(reader.GetOrdinal("SoSealHaiQuan"))) entity.SoSealHaiQuan = reader.GetString(reader.GetOrdinal("SoSealHaiQuan"));
				if (!reader.IsDBNull(reader.GetOrdinal("GhiChu"))) entity.GhiChu = reader.GetString(reader.GetOrdinal("GhiChu"));
				collection.Add(entity);
			}
			
			reader.Close();
			return collection;			
		}
		
		//---------------------------------------------------------------------------------------------
		
		// Select by foreign key return collection		
		public static IList<HangHoa> SelectCollectionBy_SoToKhai_AND_MaHaiQuan_AND_MaLoaiHinh_AND_NamDangKy(int soToKhai, string maHaiQuan, string maLoaiHinh, int namDangKy)
		{
			IList<HangHoa> collection = new List<HangHoa>();
            IDataReader reader = SelectReaderBy_SoToKhai_AND_MaHaiQuan_AND_MaLoaiHinh_AND_NamDangKy(soToKhai, maHaiQuan, maLoaiHinh, namDangKy);
			while (reader.Read())
			{
				HangHoa entity = new HangHoa();
				if (!reader.IsDBNull(reader.GetOrdinal("SoToKhai"))) entity.SoToKhai = reader.GetInt32(reader.GetOrdinal("SoToKhai"));
				if (!reader.IsDBNull(reader.GetOrdinal("MaHaiQuan"))) entity.MaHaiQuan = reader.GetString(reader.GetOrdinal("MaHaiQuan"));
				if (!reader.IsDBNull(reader.GetOrdinal("MaLoaiHinh"))) entity.MaLoaiHinh = reader.GetString(reader.GetOrdinal("MaLoaiHinh"));
				if (!reader.IsDBNull(reader.GetOrdinal("NamDangKy"))) entity.NamDangKy = reader.GetInt32(reader.GetOrdinal("NamDangKy"));
				if (!reader.IsDBNull(reader.GetOrdinal("STT"))) entity.STT = reader.GetInt32(reader.GetOrdinal("STT"));
				if (!reader.IsDBNull(reader.GetOrdinal("SoHieuContainer"))) entity.SoHieuContainer = reader.GetString(reader.GetOrdinal("SoHieuContainer"));
				if (!reader.IsDBNull(reader.GetOrdinal("SoSealContainer"))) entity.SoSealContainer = reader.GetString(reader.GetOrdinal("SoSealContainer"));
				if (!reader.IsDBNull(reader.GetOrdinal("SoSealHaiQuan"))) entity.SoSealHaiQuan = reader.GetString(reader.GetOrdinal("SoSealHaiQuan"));
				if (!reader.IsDBNull(reader.GetOrdinal("GhiChu"))) entity.GhiChu = reader.GetString(reader.GetOrdinal("GhiChu"));
				collection.Add(entity);
			}
			reader.Close();
			return collection;
		}		
		//---------------------------------------------------------------------------------------------
		
		public static DataSet SelectBy_SoToKhai_AND_MaHaiQuan_AND_MaLoaiHinh_AND_NamDangKy(int soToKhai, string maHaiQuan, string maLoaiHinh, int namDangKy)
		{
			const string spName = "[dbo].[p_HAIQUAN_QLHS_HangHoa_SelectBy_SoToKhai_AND_MaHaiQuan_AND_MaLoaiHinh_AND_NamDangKy]";
            SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);
			
			db.AddInParameter(dbCommand, "@SoToKhai", SqlDbType.Int, soToKhai);
			db.AddInParameter(dbCommand, "@MaHaiQuan", SqlDbType.Char, maHaiQuan);
			db.AddInParameter(dbCommand, "@MaLoaiHinh", SqlDbType.Char, maLoaiHinh);
			db.AddInParameter(dbCommand, "@NamDangKy", SqlDbType.Int, namDangKy);
						
            return db.ExecuteDataSet(dbCommand);
		}
		//---------------------------------------------------------------------------------------------

		public static DataSet SelectAll()
        {
            const string spName = "[dbo].[p_HAIQUAN_QLHS_HangHoa_SelectAll]";
            SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);

			
            return db.ExecuteDataSet(dbCommand);
        }
		
		//---------------------------------------------------------------------------------------------
		
		public static DataSet SelectDynamic(string whereCondition, string orderByExpression)
		{
            const string spName = "[dbo].[p_HAIQUAN_QLHS_HangHoa_SelectDynamic]";
            SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);

			db.AddInParameter(dbCommand, "@WhereCondition", SqlDbType.NVarChar, whereCondition);
            db.AddInParameter(dbCommand, "@OrderByExpression", SqlDbType.NVarChar, orderByExpression);
            
            return db.ExecuteDataSet(dbCommand);        				
		}
		
		//---------------------------------------------------------------------------------------------
				
		public static IDataReader SelectReaderAll()
        {
            const string spName = "[dbo].[p_HAIQUAN_QLHS_HangHoa_SelectAll]";
            SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);
			
            return db.ExecuteReader(dbCommand);
        }
		
		//---------------------------------------------------------------------------------------------
		
		public static IDataReader SelectReaderDynamic(string whereCondition, string orderByExpression)
		{
            const string spName = "[dbo].[p_HAIQUAN_QLHS_HangHoa_SelectDynamic]";
            SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);

			db.AddInParameter(dbCommand, "@WhereCondition", SqlDbType.NVarChar, whereCondition);
            db.AddInParameter(dbCommand, "@OrderByExpression", SqlDbType.NVarChar, orderByExpression);
            
            return db.ExecuteReader(dbCommand);        				
		}
		
		//---------------------------------------------------------------------------------------------
		
		// Select by foreign key return collection		
		public static IDataReader SelectReaderBy_SoToKhai_AND_MaHaiQuan_AND_MaLoaiHinh_AND_NamDangKy(int soToKhai, string maHaiQuan, string maLoaiHinh, int namDangKy)
		{
			const string spName = "p_HAIQUAN_QLHS_HangHoa_SelectBy_SoToKhai_AND_MaHaiQuan_AND_MaLoaiHinh_AND_NamDangKy";
            SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
			SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);
			
			db.AddInParameter(dbCommand, "@SoToKhai", SqlDbType.Int, soToKhai);
			db.AddInParameter(dbCommand, "@MaHaiQuan", SqlDbType.Char, maHaiQuan);
			db.AddInParameter(dbCommand, "@MaLoaiHinh", SqlDbType.Char, maLoaiHinh);
			db.AddInParameter(dbCommand, "@NamDangKy", SqlDbType.Int, namDangKy);
			
            return db.ExecuteReader(dbCommand);
		}		
		//---------------------------------------------------------------------------------------------
		
		#endregion
		
		//---------------------------------------------------------------------------------------------
		
		#region Insert methods.
		
		public static int InsertHangHoa(int soToKhai, string maHaiQuan, string maLoaiHinh, int namDangKy, int sTT, string soHieuContainer, string soSealContainer, string soSealHaiQuan, string ghiChu)
		{
			HangHoa entity = new HangHoa();	
			entity.SoToKhai = soToKhai;
			entity.MaHaiQuan = maHaiQuan;
			entity.MaLoaiHinh = maLoaiHinh;
			entity.NamDangKy = namDangKy;
			entity.STT = sTT;
			entity.SoHieuContainer = soHieuContainer;
			entity.SoSealContainer = soSealContainer;
			entity.SoSealHaiQuan = soSealHaiQuan;
			entity.GhiChu = ghiChu;
			return entity.Insert();
		}
		
		public int Insert()
		{
			return this.Insert(null);
		}		
		
		//---------------------------------------------------------------------------------------------
		
		public int Insert(SqlTransaction transaction)
		{			
			const string spName = "[dbo].[p_HAIQUAN_QLHS_HangHoa_Insert]";		
            SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);

			db.AddInParameter(dbCommand, "@SoToKhai", SqlDbType.Int, SoToKhai);
			db.AddInParameter(dbCommand, "@MaHaiQuan", SqlDbType.Char, MaHaiQuan);
			db.AddInParameter(dbCommand, "@MaLoaiHinh", SqlDbType.Char, MaLoaiHinh);
			db.AddInParameter(dbCommand, "@NamDangKy", SqlDbType.Int, NamDangKy);
			db.AddInParameter(dbCommand, "@STT", SqlDbType.Int, STT);
			db.AddInParameter(dbCommand, "@SoHieuContainer", SqlDbType.NVarChar, SoHieuContainer);
			db.AddInParameter(dbCommand, "@SoSealContainer", SqlDbType.NVarChar, SoSealContainer);
			db.AddInParameter(dbCommand, "@SoSealHaiQuan", SqlDbType.NVarChar, SoSealHaiQuan);
			db.AddInParameter(dbCommand, "@GhiChu", SqlDbType.NVarChar, GhiChu);
			
			if (transaction != null)
			{
				return db.ExecuteNonQuery(dbCommand, transaction);
			}
            else
			{
				return db.ExecuteNonQuery(dbCommand);
			}			
		}
				
		//---------------------------------------------------------------------------------------------
		public static bool InsertCollection(IList<HangHoa> collection)
        {
            bool ret;
			SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
            using (SqlConnection connection = (SqlConnection)db.CreateConnection())
            {
                connection.Open();
                using(SqlTransaction transaction = connection.BeginTransaction())
				{
					try
					{
						bool ret01 = true;
						foreach (HangHoa item in collection)
						{
							if (item.Insert(transaction) <= 0)
							{							
								ret01 = false;
								break;
							}
						}
						if (ret01)
						{
							transaction.Commit();
							ret = true;
						}
						else
						{
							transaction.Rollback();
							ret = false;                    	
						}
					}
					catch (Exception ex)
					{
						transaction.Rollback();
						throw new Exception("Error at InsertCollection method: " + ex.Message);
					}
					finally 
					{
						connection.Close();
					}
				}
            }
            return ret;		
		}
		
		#endregion
		
		//---------------------------------------------------------------------------------------------
		
		#region Insert / Update methods.
		
		public static int InsertUpdateHangHoa(int soToKhai, string maHaiQuan, string maLoaiHinh, int namDangKy, int sTT, string soHieuContainer, string soSealContainer, string soSealHaiQuan, string ghiChu)
		{
			HangHoa entity = new HangHoa();			
			entity.SoToKhai = soToKhai;
			entity.MaHaiQuan = maHaiQuan;
			entity.MaLoaiHinh = maLoaiHinh;
			entity.NamDangKy = namDangKy;
			entity.STT = sTT;
			entity.SoHieuContainer = soHieuContainer;
			entity.SoSealContainer = soSealContainer;
			entity.SoSealHaiQuan = soSealHaiQuan;
			entity.GhiChu = ghiChu;
			return entity.InsertUpdate();
		}
		
		//---------------------------------------------------------------------------------------------
		
		public int InsertUpdate()
		{
			return this.InsertUpdate(null);
		}
		
		//---------------------------------------------------------------------------------------------

		public int InsertUpdate(SqlTransaction transaction)
		{			
			const string spName = "p_HAIQUAN_QLHS_HangHoa_InsertUpdate";		
            SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
			SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);

			db.AddInParameter(dbCommand, "@SoToKhai", SqlDbType.Int, SoToKhai);
			db.AddInParameter(dbCommand, "@MaHaiQuan", SqlDbType.Char, MaHaiQuan);
			db.AddInParameter(dbCommand, "@MaLoaiHinh", SqlDbType.Char, MaLoaiHinh);
			db.AddInParameter(dbCommand, "@NamDangKy", SqlDbType.Int, NamDangKy);
			db.AddInParameter(dbCommand, "@STT", SqlDbType.Int, STT);
			db.AddInParameter(dbCommand, "@SoHieuContainer", SqlDbType.NVarChar, SoHieuContainer);
			db.AddInParameter(dbCommand, "@SoSealContainer", SqlDbType.NVarChar, SoSealContainer);
			db.AddInParameter(dbCommand, "@SoSealHaiQuan", SqlDbType.NVarChar, SoSealHaiQuan);
			db.AddInParameter(dbCommand, "@GhiChu", SqlDbType.NVarChar, GhiChu);
			
			if (transaction != null)
                return db.ExecuteNonQuery(dbCommand, transaction);
            else
                return db.ExecuteNonQuery(dbCommand);			
		}
		
		//---------------------------------------------------------------------------------------------
		public static bool InsertUpdateCollection(IList<HangHoa> collection)
        {
            bool ret;
			SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
            using (SqlConnection connection = (SqlConnection)db.CreateConnection())
            {
                connection.Open();
                using (SqlTransaction transaction = connection.BeginTransaction())
				{
					try
					{
						bool ret01 = true;
						foreach (HangHoa item in collection)
						{
							if (item.InsertUpdate(transaction) <= 0)
							{
								ret01 = false;
								break;
							}
						}
						if (ret01)
						{
							transaction.Commit();
							ret = true;
						}
						else
						{
							transaction.Rollback();
							ret = false;                    	
						}
					}
					catch (Exception ex)
					{
						transaction.Rollback();
						throw new Exception("Error at InsertUpdateCollection method: " + ex.Message);
						
					}
					finally 
					{
						connection.Close();
					}
				}
            }
            return ret;		
		}	
		#endregion
		
		//---------------------------------------------------------------------------------------------
		
		#region Update methods.
		
		public static int UpdateHangHoa(int soToKhai, string maHaiQuan, string maLoaiHinh, int namDangKy, int sTT, string soHieuContainer, string soSealContainer, string soSealHaiQuan, string ghiChu)
		{
			HangHoa entity = new HangHoa();			
			entity.SoToKhai = soToKhai;
			entity.MaHaiQuan = maHaiQuan;
			entity.MaLoaiHinh = maLoaiHinh;
			entity.NamDangKy = namDangKy;
			entity.STT = sTT;
			entity.SoHieuContainer = soHieuContainer;
			entity.SoSealContainer = soSealContainer;
			entity.SoSealHaiQuan = soSealHaiQuan;
			entity.GhiChu = ghiChu;
			return entity.Update();
		}
		
		//---------------------------------------------------------------------------------------------
		
		public int Update()
		{
			return this.Update(null);
		}
		
		//---------------------------------------------------------------------------------------------

		public int Update(SqlTransaction transaction)
		{
			const string spName = "[dbo].[p_HAIQUAN_QLHS_HangHoa_Update]";		
			SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
			SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);

			db.AddInParameter(dbCommand, "@SoToKhai", SqlDbType.Int, SoToKhai);
			db.AddInParameter(dbCommand, "@MaHaiQuan", SqlDbType.Char, MaHaiQuan);
			db.AddInParameter(dbCommand, "@MaLoaiHinh", SqlDbType.Char, MaLoaiHinh);
			db.AddInParameter(dbCommand, "@NamDangKy", SqlDbType.Int, NamDangKy);
			db.AddInParameter(dbCommand, "@STT", SqlDbType.Int, STT);
			db.AddInParameter(dbCommand, "@SoHieuContainer", SqlDbType.NVarChar, SoHieuContainer);
			db.AddInParameter(dbCommand, "@SoSealContainer", SqlDbType.NVarChar, SoSealContainer);
			db.AddInParameter(dbCommand, "@SoSealHaiQuan", SqlDbType.NVarChar, SoSealHaiQuan);
			db.AddInParameter(dbCommand, "@GhiChu", SqlDbType.NVarChar, GhiChu);
			
			if (transaction != null)
                return db.ExecuteNonQuery(dbCommand, transaction);
            else
                return db.ExecuteNonQuery(dbCommand);
		}
				
		//---------------------------------------------------------------------------------------------
		public static bool UpdateCollection(IList<HangHoa> collection)
        {
            bool ret;
			SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
            using (SqlConnection connection = (SqlConnection)db.CreateConnection())
            {
                connection.Open();
                using (SqlTransaction transaction = connection.BeginTransaction())
				{
					try
					{
						bool ret01 = true;
						foreach (HangHoa item in collection)
						{
							if (item.Update(transaction) <= 0)
							{
								ret01 = false;
								break;
							}
						}
						if (ret01)
						{
							transaction.Commit();
							ret = true;
						}
						else
						{
							transaction.Rollback();
							ret = false;                    	
						}
					}
					catch (Exception ex)
					{
						transaction.Rollback();
						throw new Exception("Error at UpdateCollection method: " + ex.Message);
					}
					finally 
					{
						connection.Close();
					}
				}
            }
            return ret;		
		}
		
		#endregion
		
		//---------------------------------------------------------------------------------------------
		
		#region Delete methods.
		
		public static int DeleteHangHoa(int soToKhai, string maHaiQuan, string maLoaiHinh, int namDangKy, int sTT)
		{
			HangHoa entity = new HangHoa();
			entity.SoToKhai = soToKhai;
			entity.MaHaiQuan = maHaiQuan;
			entity.MaLoaiHinh = maLoaiHinh;
			entity.NamDangKy = namDangKy;
			entity.STT = sTT;
			return entity.Delete();
		}
		
		public int Delete()
		{
			return this.Delete(null);
		}
		
		//---------------------------------------------------------------------------------------------

		public int Delete(SqlTransaction transaction)
		{
			const string spName = "[dbo].[p_HAIQUAN_QLHS_HangHoa_Delete]";		
			SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
			SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);
			
			db.AddInParameter(dbCommand, "@SoToKhai", SqlDbType.Int, SoToKhai);
			db.AddInParameter(dbCommand, "@MaHaiQuan", SqlDbType.Char, MaHaiQuan);
			db.AddInParameter(dbCommand, "@MaLoaiHinh", SqlDbType.Char, MaLoaiHinh);
			db.AddInParameter(dbCommand, "@NamDangKy", SqlDbType.Int, NamDangKy);
			db.AddInParameter(dbCommand, "@STT", SqlDbType.Int, STT);
			
			if (transaction != null)
                return db.ExecuteNonQuery(dbCommand, transaction);
            else
                return db.ExecuteNonQuery(dbCommand);
		}
		
		//---------------------------------------------------------------------------------------------
		
		public static int DeleteBy_SoToKhai_AND_MaHaiQuan_AND_MaLoaiHinh_AND_NamDangKy(int soToKhai, string maHaiQuan, string maLoaiHinh, int namDangKy)
		{
			string spName = "[dbo].[p_HAIQUAN_QLHS_HangHoa_DeleteBy_SoToKhai_AND_MaHaiQuan_AND_MaLoaiHinh_AND_NamDangKy]";
            SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);
			
			db.AddInParameter(dbCommand, "@SoToKhai", SqlDbType.Int, soToKhai);
			db.AddInParameter(dbCommand, "@MaHaiQuan", SqlDbType.Char, maHaiQuan);
			db.AddInParameter(dbCommand, "@MaLoaiHinh", SqlDbType.Char, maLoaiHinh);
			db.AddInParameter(dbCommand, "@NamDangKy", SqlDbType.Int, namDangKy);
						
            return db.ExecuteNonQuery(dbCommand);
		}
		
		//---------------------------------------------------------------------------------------------
		
		public static bool DeleteCollection(IList<HangHoa> collection)
        {
            bool ret;
			SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
            using (SqlConnection connection = (SqlConnection)db.CreateConnection())
            {
                connection.Open();
                using (SqlTransaction transaction = connection.BeginTransaction())
				{
					try
					{
						bool ret01 = true;
						foreach (HangHoa item in collection)
						{
							if (item.Delete(transaction) <= 0)
							{
								ret01 = false;
								break;
							}
						}
						if (ret01)
						{
							transaction.Commit();
							ret = true;
						}
						else
						{
							transaction.Rollback();
							ret = false;                    	
						}
					}
					catch (Exception ex)
					{
						transaction.Rollback();
						throw new Exception("Error at DeleteCollection method: " + ex.Message);
					}
					finally 
					{
						connection.Close();
					}
				}
            }
            return ret;		
		}
		#endregion
	}	
}